package com.example.AIstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.DTO.RecordDTO;
import com.example.AIstudy.dao.CompositionDao;
import com.example.AIstudy.dao.RecordDetailDao;
import com.example.AIstudy.dao.TranslateDao;
import com.example.AIstudy.domain.*;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.RecordDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class RecordDetailServiceImpl extends ServiceImpl<RecordDetailDao, RecordDetail> implements RecordDetailService {

    //做题记录表对应的dao
    @Autowired
    RecordDetailDao recordDetailDao;

    //作文详情表对应的dao
    @Autowired
    CompositionDao compositionDao;

    //翻译详情表对应的dao
    @Autowired
    TranslateDao translateDao;

    //插入一条做题的详细记录
    @Transactional(rollbackFor = AIstudyException.class)
    public void insertRecordDetail(RecordDTO recordDTO) throws AIstudyException{
        try{
            //作文详情不为空，插入
            CompositionDetail detail1=recordDTO.getCompositionDetail();
            if(detail1!=null){
                compositionDao.insertCompositionDetail(detail1);
                recordDTO.setCompCommentID(detail1.getId());
            }
            //翻译详情不为空，插入
            TranslateDetail detail2=recordDTO.getTranslateDetail();
            if(detail2!=null){
                translateDao.insertTranslateDetail(detail2);
                recordDTO.setTransCommentID(detail2.getId());
            }
            //答题的基本信息
            RecordDetail detail=recordDTO;
            System.out.println(detail.stuAnswer);
            recordDetailDao.insertRecordDetail(recordDTO);
        }catch(Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加学生完成套题的详情失败");
        }
    }
    @Transactional(rollbackFor = AIstudyException.class)
    public void insertRecordDetail2(RecordDetail record) throws AIstudyException{
        try{
            recordDetailDao.insertRecordDetail(record);
        }catch(Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加学生完成套题的详情失败");
        }
    }

    //根据条件查询做题的记录，封装成DTO返回，一般都是根据套题的id来搜素
    public List<RecordDTO> findRecord(Map<String,Object> condition){

        QueryWrapper<RecordDetail> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("exerciseID"), "exerciseID", condition.get("exerciseID"));
        //学生查询答题情况的时候可以筛选
        qw.ge(condition.containsKey("minScore"), "stuScore", condition.get("minScore"));
        qw.le(condition.containsKey("maxScore"), "stuScore", condition.get("maxScore"));
        return recordDetailDao.findRecord(qw);
    }

    @Transactional(rollbackFor = AIstudyException.class)
    public void updateRecord(RecordDTO recordDTO)throws AIstudyException{
        try{
            //作文详情不为空 更新
            if(recordDTO.getCompositionDetail()!=null){
                compositionDao.updateById(recordDTO.getCompositionDetail());
            }
            //翻译详情不为空，更新
            if(recordDTO.getTranslateDetail()!=null){
                translateDao.updateById(recordDTO.getTranslateDetail());
            }
            //更新答题的基本信息
            recordDetailDao.updateById(recordDTO);
        }catch(Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,"更新学生完成套题的信息失败");
        }
    }

}
